package com.zlz.blog.server.blog.controller;

import cn.hutool.http.HttpUtil;
import com.zlz.blog.manage.common.response.ResultSet;
import com.zlz.blog.server.blog.service.BlogAttachFileService;
import com.zlz.blog.server.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * created by zlz on 2020/12/18 16:00
 **/
@RestController
@RequestMapping("/file")
public class BlogAttachFileController {

    private BlogAttachFileService blogAttachFileService;

    @Autowired
    public void setBlogAttachFileService(BlogAttachFileService blogAttachFileService){
        this.blogAttachFileService = blogAttachFileService;
    }

    /**
     * 上传图片，返回图片地址到前端
     *
     * @param file 文件
     * @throws IOException IOException
     */
    @PostMapping("/image/upload")
    public ResultSet<String> upload(MultipartFile file) throws IOException, NoSuchAlgorithmException {
        return blogAttachFileService.uploadImage(file);
    }

    /**
     * 下载文件
     *
     * @param path     文件路径
     * @param response response
     */
    @PostMapping("/download")
    public void download(String path, HttpServletResponse response) {
        blogAttachFileService.downloadFile(path, response);
    }

    @PostMapping("/image/handle")
    public void imageHandler(MultipartFile file, HttpServletResponse response) {
        try {
            String imageSegmentationUrl = ImageUtil.getImageSegmentation();
            String result = HttpUtil.get(imageSegmentationUrl);
            InputSource is = new InputSource(new StringReader(result));
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
            String nodeValue = document.getElementsByTagName("ResultImage").item(0).getFirstChild().getNodeValue();
            byte[] decode = Base64.getDecoder().decode(nodeValue);

            BufferedImage image = ImageIO.read(new ByteArrayInputStream(decode));

            BufferedImage back = new BufferedImage(
                    image.getWidth(),
                    image.getHeight(),
                    BufferedImage.TYPE_INT_RGB);

            Graphics2D graphic = back.createGraphics();
            graphic.setBackground(Color.ORANGE);
            graphic.drawImage(image, 0, 0, Color.BLACK, null);
            graphic.dispose();

            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(back, "png", outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
